#include<iostream>
#include<queue>
const int maxn=1 << 24;
using namespace std;
int next[maxn][4];
int g[maxn];
int fa[maxn];
int choice[maxn];
int ans[200];
int jg[12][4];
struct node{
    int state;
    double gz;
   node(int s):state(s){
        double h=0;
        gz=0;
        for(int i=0;i<12;i++){
            if((s>>(2*i))&3){
                h+=(4-(s>>(2*i))&3);
            }
        }
        gz=g[s]+0.8*h;
    }
    bool operator<(const node &y)const{
        return gz > y.gz;
    }
};
priority_queue<node>q;
int main(){
    int start=0;
    int button;
    for(int i=0;i<12;i++){
        cin>>button;
        start|=((button-1)<<(2*i));
        for(int j=0;j<4;j++){
            cin>>jg[i][j];
            jg[i][j]-=1;
        }
    }
    q.push(node(start));
    g[start]=0;
    while(!q.empty()){
        node f= q.top();
        q.pop();
        if(f.state==0)break;
        for(int i=0;i<12;i++){
         int dq=(f.state>>(2*i))&3;
         int nx=jg[i][dq];
         int nxt=(f.state>>(2*nx))&3;
         int state1=f.state^(dq<<(2*i))^(((dq+1)&3)<<(2*i));
         state1=state1^(nxt<<(2*nx))^(((nxt+1)&3)<<(2*nx));
         if(!g[state1]){
            g[state1]=g[f.state]+1;
            fa[state1]=f.state;
            choice[state1]=i+1;
             q.push(node(state1));
         }
        }
    }
    int state=0;
    int cnt=0;
    while(start!=state){
        ans[++cnt]=choice[state];
        state=fa[state];
    }
    cout<<cnt<<endl;
    for(int i=cnt;i;i--){
        cout<<ans[i]<<" ";
    }
    return 0;
}